package com.my.study.io.input;

import lombok.extern.slf4j.Slf4j;

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Objects;

/**
 * @author Carlos
 * @version 1.0
 * @Description TODO
 * @date 2021/6/26 0:11
 **/
@Slf4j(topic = "c.FileInputStreamTest")
public class FileInputStreamTest {

    public static void main(String[] args) {
        String path = "D:/file/hello.txt";

        test02(path);


    }

    private static void test02(String path) {
        int readLen = 0;
        FileInputStream ins = null;
        byte[] buf = new byte[8];
        try {
            ins = new FileInputStream(path);
            while ((readLen = ins.read(buf)) != -1) {
                log.debug("{}", new String(buf, 0, readLen,StandardCharsets.UTF_8));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (Objects.nonNull(ins)) {
                try {
                    ins.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    private static void test01(String path) {
        int readData = 0;
        FileInputStream ins = null;
        try {
            ins = new FileInputStream(path);
            while ((readData = ins.read()) != -1) {
                log.debug("{} {}",readData, (char) readData);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (Objects.nonNull(ins)) {
                try {
                    ins.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
